// pages/detail/detail.js
const {
  ShopShow,
  ShopApply,
} = require('../../utils/api')
import confg from '../../utils/config';
import util from '../../utils/util';
import $share from '../../utils/codeshare';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    confg: confg,
    detail: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.scene){
      this.setData({id:options.scene})
    }else{
      this.setData(options)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail()
  },
  // 获取详情数据
  getDetail() {
    let obj = {
      id: this.data.id
    };
    ShopShow(obj).then(res => {
      res.data.platform = `/images/${res.data.platform}-s.png`;
      res.data.commerce.slug = `/images/shop/${res.data.commerce.slug}.png`;
      this.setData({
        detail: res.data
      })
      wx.setNavigationBarTitle({
        title: res.data.product_name
      })
    })
  },
  reportFn() {
    util.checkLogin(re => {
      let ob = {
        id: this.data.id
      };
      ShopApply(ob).then(res => {
        if (res.code == 200) {
          this.getDetail()
          wx.showToast({
            title: res.info,
            icon: 'success',
            duration: 1500
          })
        } else {
          wx.showToast({
            title: res.info,
            icon: 'none',
            duration: 1500
          })
        }
      })
    },2)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    const data = this.data.detail;
    const shares = {
      title: data.product_name
    }
    return shares
  },
  viewImg(e) {
    let ind = e.currentTarget.dataset.ind, imgs = this.data.detail.images,urls = [];
    imgs.forEach(el=>{
      urls.push(confg.base_url + el)
    })
    wx.previewImage({
      current: urls[ind],
      urls: urls
    });
  },
  goHome(e) {
    // wx.switchTab({
    //   url: '/pages/merchart/merchart',
    // })
    wx.switchTab({
      url: '/pages/merchant/merchant',
    })
  },
  chareCodeFn(e) {
    $share.fns('taskcanvas','shop', this)
  },
  copyContent(e) {
    wx.setClipboardData({
      data: this.data.detail.mobile,
      success (res) {
        wx.getClipboardData({
          success (res) { }
        })
      }
    })
  }
})